package com.abc.robot01.activity.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.GenericShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import com.abc.robot01.R
import kotlinx.coroutines.delay

class LaunchedEffectDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val scrollState = rememberScrollState()
            Column(
                modifier = Modifier
                    .verticalScroll(scrollState)
                    .fillMaxSize()
            ) {
                CoroutineExample()
                Spacer(modifier = Modifier.height(2.dp))
                DataFetchingComposable()
                Spacer(modifier = Modifier.height(2.dp))
                DisplayData()
                Spacer(modifier = Modifier.height(2.dp))
                CounterWithExternalDisplay()
                Spacer(modifier = Modifier.height(2.dp))
            }
        }
    }
}

@Composable
fun DisplayCount(count: State<Int>) {
    Text(text = "Count: ${count.value}")
}

@Composable
fun CounterWithExternalDisplay() {
    val count = remember { mutableStateOf(0) }
    Column {
        DisplayCount(count)  // Pass the state to another composable
        Button(onClick = { count.value++ }) {
            Text(text = "Increment")
        }
    }
}


@Composable
fun DataProducer(): State<String> {
    // Produce state with an initial value of "Loading..."
    return produceState(initialValue = "Loading...") {
        // Simulate a network delay
        delay(3000)
        // After the delay, update the state value
        value = "Data fetched!"
    }
}

@Composable
fun DisplayData() {
    // Get the state produced by DataProducer
    val data = DataProducer()

    // Use the state value in the UI
    Text(text = data.value)
}


@Composable
fun DataFetchingComposable() {
    var data by remember { mutableStateOf("Loading...") }

    // Fetch data when the composable enters the composition
    LaunchedEffect(Unit) {
        val result = fetchDataFromNetwork()
        data = result
    }

    // Display the fetched data
    Text(text = data)
}

suspend fun fetchDataFromNetwork(): String {
    delay(3000) // Simulate network delay
    return "Data fetched!"
}


@Composable
fun CoroutineExample() {
    // Use a MutableState to track the text
    var text by remember { mutableStateOf("Hello, Jetpack Compose!") }

    // LaunchedEffect launches a coroutine tied to the Composable's lifecycle
    LaunchedEffect(Unit) {
        delay(2000L) // Delay for 2 seconds
        text = "Coroutine has finished!"
    }

    // Display the text in a Text composable
    Text(text = text)
}

